package com.example.administrator.androidros.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.example.administrator.androidros.R;
import com.example.administrator.androidros.adapter.SceneAdapter;
import com.example.administrator.androidros.base.BaseActivity;
import com.example.administrator.androidros.event.MessageEvent;
import com.hhc.netty.DeviceModel;
import com.example.administrator.androidros.net.HttpUtils;
import com.example.administrator.androidros.utils.AgentConstants;
import com.example.administrator.androidros.utils.AgentSharedPreferences;
import com.example.administrator.androidros.utils.ServerConstants;
import com.example.administrator.androidros.utils.UUIDUtil;
import com.example.administrator.androidros.widget.AppendDialog;
import com.example.administrator.androidros.widget.CustomTipDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SceneActivity extends BaseActivity {
    @BindView(R.id.activity_scene_list)
    protected RecyclerView activity_scene_list;

    protected SceneAdapter sceneAdapter;
    protected List<DeviceModel> data = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_scene);
    }

    @Override
    protected void initData() {
        initCommonHeader();
        initScene();
    }

    @Override
    protected void onFailure(int requestCode, Object object) {
        showToast(R.string.common_response_failure);
        stopTipDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object) {
        stopTipDialog();
        switch (requestCode) {
            case 1001:
                if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                    showToast(HttpUtils.getResultMessage(object));
//                    EventBus.getDefault().post(new MessageEvent());
                    getSceneList();
                } else {
                    showToast(HttpUtils.getResultMessage(object));
                }
                break;
            case 1002:
                //获取场景列表
                if (object != null) {
                    DeviceModel[] deviceModels = (DeviceModel[]) HttpUtils.getData(object, DeviceModel[].class);
                    data = Arrays.asList(deviceModels);
                    sceneAdapter.setNewData(data);
                }
                break;
            default:
                break;
        }
    }

    protected void initCommonHeader() {
        configCommonHeader(this, getString(R.string.scene_title), true);
    }

    protected void initScene() {
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        activity_scene_list.setLayoutManager(mLayoutManager);
        sceneAdapter = new SceneAdapter(this, data);
        activity_scene_list.setAdapter(sceneAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        getSceneList();
    }

    protected void getSceneList() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("memberId", AgentSharedPreferences.getLastLoginId(mBaseActivity));
        HttpUtils.post(mBaseActivity, 1002, ServerConstants.QUERY_SCENE_LIST, jsonObject.toString(), mBaseActivity);
        startTipDialog(CustomTipDialog.ICON_TYPE_LOADING, getString(R.string.common_loading), true);
    }

    @OnClick(R.id.common_head_more)
    protected void moreClick() {
        AppendDialog appendDialog = new AppendDialog(mBaseActivity);
//        appendDialog.setSceneType("房间");
        appendDialog.setOnAddClickListener(new AppendDialog.OnAddClickListener() {
            @Override
            public void onAddClick(String sceneType, String sceneName) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("memberId", AgentSharedPreferences.getLastLoginId(mBaseActivity));
                jsonObject.put("masterId", UUIDUtil.get36UUID());
                jsonObject.put("sceneId", UUIDUtil.get8UUID());
                jsonObject.put("sceneType", sceneType);
                jsonObject.put("sceneName", sceneName);
                HttpUtils.post(mBaseActivity, 1001, ServerConstants.ADD_SCENE, jsonObject.toString(), mBaseActivity);
                startTipDialog(CustomTipDialog.ICON_TYPE_LOADING, getString(R.string.common_adding), true);
            }
        });
        appendDialog.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        getSceneList();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }


    @Override
    public void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
